﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement.Mine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GameStateManagement.Screens.Base
{
    public class ButtonScreen : CommandScreen
    {
        #region Fields

        List<MyButton> buttons = new List<MyButton>();
        int selectedEntry = 0;
        string menuTitle;

        #endregion

        #region Properties






        protected IList<MyButton> Buttons
        {
            get { return buttons; }
        }


        #endregion

        #region Initialization


        public override void LoadContent()
        {
            base.LoadContent();
            //menuEntries[0].Ready += new EventHandler<MyEventArgs>(ButtonScreen_Ready);
        }


        public ButtonScreen(string menuTitle)
        {
            this.menuTitle = menuTitle;

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }


        #endregion



        protected virtual void UpdateMenuEntryLocations()
        {
            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // start at Y = 175; each X value is generated per entry
            Vector2 position = new Vector2(0f, 285f);

            // update each menu entry's location in turn
            for (int i = 0; i < buttons.Count; i++)
            {
                MyButton menuEntry = buttons[i];

                // each entry is to be centered horizontally
                position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;

                if (ScreenState == ScreenState.TransitionOn)
                    position.X -= transitionOffset * 256;
                else
                    position.X += transitionOffset * 512;

                // set the entry's position
                menuEntry.Position = position;

                // move down for the next entry the size of this entry
                position.Y += menuEntry.GetHeight(this);
            }
        }

        public override void HandleInput(InputState input)
        {
            if (input.IsMenuUp(ControllingPlayer))
            {
                selectedEntry--;
                if (selectedEntry < 0)
                    selectedEntry = buttons.Count - 1;
                OnReady(selectedEntry);
            }

            if (input.IsMenuDown(ControllingPlayer))
            {
                selectedEntry++;
                if (selectedEntry >= buttons.Count)
                    selectedEntry = 0;
                OnReady(selectedEntry);
            }

            PlayerIndex playerIndex;

            if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
            {
                OnSelectEntry(selectedEntry, playerIndex);
            }
            else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
            {
                OnCancel(playerIndex);
            }
        }

        protected override void OnReady(int selectedEntry)
        {
            buttons[selectedEntry].OnReady();
        }

        protected override void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
        {
            buttons[entryIndex].OnSelectEntry(playerIndex);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // Update each nested MenuEntry object.
            for (int i = 0; i < buttons.Count; i++)
            {
                bool isSelected = IsActive && (i == selectedEntry);

                buttons[i].Update(this, isSelected, gameTime);
            }
        }





        public override void Draw(GameTime gameTime)
        {
            // make sure our entries are in the right place before we draw them
            UpdateMenuEntryLocations();

            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            spriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < buttons.Count; i++)
            {
                MyButton menuEntry = buttons[i];

                bool isSelected = IsActive && (i == selectedEntry);

                menuEntry.Draw(this, isSelected, gameTime);
            }

            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 180);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = Color.Green * TransitionAlpha;
            float titleScale = 1.25f;

            titlePosition.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }


    }
}
